const httpStatus = require('http-status');
const catchAsync = require('../utils/catchAsync');
const recipeService = require('../services/recipe.service');
const pick = require('../utils/pick');
const Recipe = require('../models/recipe.model');
const { validateRecipe } = require('../validators/recipe.validator');
const ApiError = require('../utils/ApiError');

/**
 * 获取食谱列表
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const getRecipes = catchAsync(async (req, res) => {
  const filter = pick(req.query, ['category._id', 'tags', 'difficulty', 'isVerified']);
  const options = pick(req.query, ['sortBy', 'limit', 'page']);

  const result = await recipeService.getRecipes(filter, options);

  res.send({
    error: 0,
    body: result,
    message: '获取食谱列表成功'
  });
});

/**
 * 创建食谱
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const createRecipe = catchAsync(async (req, res) => {
  // 验证请求数据
  const validationResult = await validateRecipe(req.body);
  if (!validationResult.isValid) {
    throw new ApiError(httpStatus.BAD_REQUEST, validationResult.message);
  }

  // 设置作者信息
  const recipeData = {
    ...req.body,
    author: {
      _id: req.user._id,
      name: req.user.name
    }
  };

  // 创建食谱
  const recipe = await recipeService.createRecipe(recipeData);

  res.status(httpStatus.CREATED).json({
    error: 0,
    body: recipe,
    message: '食谱创建成功'
  });
});

/**
 * 更新食谱
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const updateRecipe = catchAsync(async (req, res) => {
  const { recipeId } = req.params;
  
  // 验证请求数据
  const validationResult = await validateRecipe(req.body, true);
  if (!validationResult.isValid) {
    throw new ApiError(httpStatus.BAD_REQUEST, validationResult.message);
  }

  // 更新食谱
  const recipe = await recipeService.updateRecipe(recipeId, req.body);

  res.json({
    error: 0,
    body: recipe,
    message: '食谱更新成功'
  });
});

/**
 * 获取食谱详情
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const getRecipe = catchAsync(async (req, res) => {
  const { recipeId } = req.params;
  const recipe = await recipeService.getRecipeById(recipeId);

  res.json({
    error: 0,
    body: recipe,
    message: '获取食谱详情成功'
  });
});

/**
 * 删除食谱
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const deleteRecipe = catchAsync(async (req, res) => {
  const { recipeId } = req.params;
  await recipeService.deleteRecipe(recipeId);

  res.json({
    error: 0,
    body: null,
    message: '食谱删除成功'
  });
});

/**
 * 软删除食谱（标记为已删除）
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const softDeleteRecipe = catchAsync(async (req, res) => {
  const { recipeId } = req.params;
  await recipeService.softDeleteRecipe(recipeId);

  res.json({
    error: 0,
    body: null,
    message: '食谱已软删除'
  });
});

/**
 * 恢复已删除的食谱
 * @param {Object} req - Express请求对象
 * @param {Object} res - Express响应对象
 */
const restoreRecipe = catchAsync(async (req, res) => {
  const { recipeId } = req.params;
  const recipe = await recipeService.restoreRecipe(recipeId);

  res.json({
    error: 0,
    body: recipe,
    message: '食谱已恢复'
  });
});

module.exports = {
  getRecipes,
  createRecipe,
  updateRecipe,
  getRecipe,
  deleteRecipe,
  softDeleteRecipe,
  restoreRecipe
}; 